package com.sgb.capital.utils;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;

import androidx.annotation.NonNull;

import org.jetbrains.annotations.NotNull;

/**
 * Spannable 工具类
 * @author cyj
 */
public class SpannableUtils {

    /** 下划线 underline */
    public static SpannableString changePartTextColorAndUnderline(String content, String colorStr, String underlineContent) {
        int underlineIndex = content.indexOf(underlineContent);
        SpannableString spannableString = changePartTextColor(content, colorStr, 0, content.length(), false);
        if(underlineIndex > 0) {
            spannableString.setSpan(new UnderlineSpan() {
                @Override
                public void updateDrawState(@NonNull @NotNull TextPaint ds) {
                    ds.setColor(Color.WHITE);
                    super.updateDrawState(ds);
                }
            }, underlineIndex, underlineIndex + underlineContent.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    public static SpannableString changePartTextColor(String content, String colorStr1, String colorStr2, boolean change, int start) {
        if(change) {
            return changePartTextColor(content, colorStr1, start, content.length(), false);
        } else {
            return changePartTextColor(content, colorStr2, start, content.length(), false);
        }
    }

    public static SpannableString changePartTextColor(String content, String colorStr) {
        return changePartTextColor(content, colorStr, 0, content.length(), false);
    }

    public static SpannableString changePartTextColor(String content, String colorStr, int start) {
        return changePartTextColor(content, colorStr, start, content.length(), false);
    }

    public static SpannableString changePartTextColor(String content, String colorStr, int start, int end) {
        return changePartTextColor(content, colorStr, start, end, false);
    }

    /**
     * Spannable改变部分文字颜色
     * @param content    内容
     * @param colorStr   颜色字符串，如"FF4B10"
     * @param start      颜色改变开始位置
     * @param end        颜色改变结束位置
     * @return  SpannableString
     */
    public static SpannableString changePartTextColor(String content, String colorStr, int start, int end, boolean isBold) {
        SpannableString spanString = new SpannableString(content);
        if(!StringUtils.isEmpty(colorStr)) {
            spanString.setSpan(new ForegroundColorSpan(Color.parseColor(colorStr)),
                    start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if(isBold) {
            spanString.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spanString;// StyleSpan styleSpan_B  = new StyleSpan(Typeface.BOLD);
    }

    /**
     * Spannable改变最后一个文字颜色
     * @param content    内容
     * @param colorStr   颜色字符串，如"FF4B10"
     * @return  SpannableString
     */
    public static SpannableString changePartTextColorForLastOnce(String content, String colorStr) {
        return changePartTextColor(content, colorStr, content.length() - 1, content.length());
    }
}
